package com.lemon.stock.util

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.lemon.stock.app.AppPreferences
import com.lemon.stock.entity.User
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

object AuthManager {
    private var _user = MutableLiveData<User>()
    val user:LiveData<User> = _user

    fun init(){
        GlobalScope.launch {
            withContext(Dispatchers.Main){
                _user.value = AppPreferences.getUser()

            }
        }
    }

    fun onLogin(user: User) {
        GlobalScope.launch {
            _user.postValue(user)
            AppPreferences.setUser(user)
        }
    }

}